package com.hzlj.position.locate.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.position.config.common.dto.common.ReportLocateDTO;
import com.hzlj.position.locate.common.dto.reportCommon.ReportHandleDTO;
import com.hzlj.position.locate.common.dto.reportInitiative.ReportInitiativeQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInitiative.ReportInitiativeWithJzJbxxDTO;
import com.hzlj.position.locate.service.ReportInitiativeService;
import com.hzlj.sqjz.config.common.dto.common.handle.HandleCommonQueryResultDTO;
import com.hzlj.sqjz.system.service.JgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【通信联络核查-人脸签到】接口
 *
 * @author lifh
 * @date 2023-03-13 19:10:11
 */
@Slf4j
@RestController
@RequestMapping("/position/report/initiative")
public class ReportInitiativeController {
    @Resource
    private ReportInitiativeService reportInitiativeService;
    @Resource
    private JgService jgService;


    /**
     * 处理异常
     */
    @Transactional
    @PostMapping("/handle")
    public ResultDTO<Boolean> handle(@RequestBody ReportHandleDTO dto) {
        //更新分表
        this.reportInitiativeService.handle(dto);
        return ResultDTO.ok();
    }


    /**
     * 获取处理状态数据
     */
    @GetMapping(value = "/handleResult/{id}")
    public ResultDTO<HandleCommonQueryResultDTO> getHandleResult(@PathVariable("id") String id, Date belongDay) {
        log.info("【获取处理状态】id={},belongDay={}", id, belongDay);
        return new ResultDTO<>(
                reportInitiativeService.getHandleResult(id, belongDay)
        );
    }

    /**
     * 上报位置
     */
    @PutMapping("")
    public ResultDTO<Boolean> saveReportInitiative(@RequestBody ReportLocateDTO dto) {
        log.info("人脸签到：dto={}", JsonUtils.obj2json(dto));
        reportInitiativeService.reportLocate(dto);
        return new ResultDTO<>();
    }

    /**
     * 导出 通信联络核查-人脸签到
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       ReportInitiativeQueryParamDTO dto) {
        List<ReportInitiativeWithJzJbxxDTO> list = reportInitiativeService.listReportInitiative(dto);
        //将明细的记录展示处理
        List<ReportInitiativeWithJzJbxxDTO> signedList = list.stream().filter(e -> ObjectUtil.isNotEmpty(e.getReportTime())).flatMap(e -> {
            return e.getReportTimes().stream()
                    .sorted((a, b) -> b.getEndTime().compareTo(a.getEndTime()))
                    .flatMap(p -> p.getPositions().stream())
                    .sorted((a, b) -> b.getReportTime().compareTo(a.getReportTime()))
                    .map(p -> {
                        ReportInitiativeWithJzJbxxDTO report = BeanUtil.copyProperties(e, ReportInitiativeWithJzJbxxDTO.class);
                        report.setReportAddress(p.getReportAddress());
                        report.setReportTime(p.getReportTime());
                        return report;
                    });
        }).collect(Collectors.toList());
        //加上未签到的
        signedList.addAll(
                list.stream().filter(e -> ObjectUtil.isEmpty(e.getReportTime())).collect(Collectors.toList())
        );

        //导出明细
        EasyExcelUtil.downCustomerExcel(response,
                "人脸签到记录",
                "人脸签到记录",
                jgService.export3Level(ReportInitiativeWithJzJbxxDTO::getJgCode, ReportInitiativeWithJzJbxxDTO::getJgMc)
                        .add(ReportInitiativeWithJzJbxxDTO::getXm, "姓名")
                        .add(ReportInitiativeWithJzJbxxDTO::getXbText, "性别")
                        .add(ReportInitiativeWithJzJbxxDTO::getReportTime, "上报时间")
                        .add(ReportInitiativeWithJzJbxxDTO::getReportStatusText, "状态")
                        .add(ReportInitiativeWithJzJbxxDTO::getReportAddress, "位置信息")
                        .getFields(),
                signedList
        );
    }


    /**
     * 同步人脸签到
     */
    @GetMapping("/sync")
    public ResultDTO<Boolean> sync() {
        reportInitiativeService.sync();
        return new ResultDTO<>();
    }

    /**
     * 列表查询 通信联络核查-人脸签到
     */
    @GetMapping("/list")
    public ResultDTO<List<ReportInitiativeWithJzJbxxDTO>> listReportInitiative(ReportInitiativeQueryParamDTO dto) {
        return new ResultDTO<>(reportInitiativeService.listReportInitiative(dto));
    }

    /**
     * 分页查询 通信联络核查-人脸签到
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<ReportInitiativeWithJzJbxxDTO>> pageReportInitiative(ReportInitiativeQueryParamDTO dto, PageParamDTO pageParamDTO) {
        return new ResultDTO<>(reportInitiativeService.pageReportInitiative(dto, pageParamDTO));
    }


    /**
     * 同步
     */
    @GetMapping("/sync/{jzId}")
    public ResultDTO<Boolean> saveReportInitiative(@PathVariable("jzId") String jzId) {
        reportInitiativeService.sync(jzId);
        return new ResultDTO<>();
    }

}

